package badugajunit;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Test;
import badugaserver.*;

public class ServersTests 
{

	private Table t;
	private Card card;
	private Deck deck;

	@After
	public void tearDown() throws Exception 
	{
		t = null;
		card = null;
		deck = null;
	}

	@Test
	public void testRun() 
	{
		t = new Table (4, 15, 10);
		assertNotNull(t);
	}

	@Test
	public void testSettingHand()
	{
		deck = new Deck();
		assertNotNull(deck);
		Card c = deck.drawFromDeck();
		assertNotNull(c);
		Hand hand = new Hand(deck);
		assertNotNull(hand);
		hand.removeCard(1);
		hand.setCard(1, c);
		assertEquals("It's dying!", hand.getCard(1), c);
	}
	
	@Test
	public void testSetSharedWorker() 
	{
		WorkerRunnable[] sw = new WorkerRunnable [10];
		fail("'Cause this all was planned?");
	}
	
	@Test(expected = BadugaExceptions.class)
	public void testCardConstructor() throws BadugaExceptions
	{
		int rank = 0, suit = 0;
		card = new Card(rank, suit);
		fail();
	}
	
	@Test
	public void testCardtoString() throws BadugaExceptions
	{
		int rank = 1, suit = 1;
		boolean shortstring = false;
		boolean longstring = true;
		card = new Card(rank, suit);
		if(!card.toString(shortstring).equals("As"))
		{
			fail("If it fails, then it works :)");
		}
		if(!card.toString(longstring).equals("Ace of hearts"))
		{
			fail();
		}
	}
	
	@Test
	public void testDeckConstructor() throws BadugaExceptions
	{
		deck = new Deck();
		Card card;
		Card temp = deck.drawFromDeck();
		for(int i = 1; i<= 4; i++)
		{
			if(i == 5)
			{
				fail();
			}
			else
			{
				for(int j = 1; j <= 13; j++)
				{
					card = new Card(j,i);
					if(card.toString().equals(temp.toString()))
					{
						break;
					}
				}
			}
		}
		
		assertEquals("It's dying!", deck.deckSize(), 52);
	}
	
	@Test
	public void testDeckSize()
	{
		deck = new Deck();
		assertNotEquals(deck, 52);
	}
	
	@Test
	public void testShuffle()
	{
		deck = new Deck();
		Deck temp = deck;
		int counter = 0;
		temp.shuffleDeck();
		for(int i=0; i<deck.deckSize(); i++)
		{
			if(deck.drawFromDeck() == temp.drawFromDeck())
			{
				counter++;
			}
		}
		if(counter > 10)
		{
			fail();
		}
	}

	@Test
	public void testSwap() throws BadugaExceptions
	{
		deck = new Deck();
		Deck temp = deck;
		Card card_1 = new Card(1,1);
		Card card_2 = temp.drawFromDeck();
		card_1 = deck.swapCard(card_1);
		assertEquals(card_1, card_2);
		
		
	}
}
